﻿namespace PillBox.Interactivity
{
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Interactivity;

    public class SwipeGestureTrigger : TriggerBase<FrameworkElement>
    {
        protected override void OnAttached()
        {
            base.OnAttached();

            this.AssociatedObject.ManipulationStarted += AssociatedObject_ManipulationStarted;
            this.AssociatedObject.ManipulationDelta += AssociatedObject_ManipulationDelta;
            this.AssociatedObject.ManipulationCompleted += AssociatedObject_ManipulationCompleted;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();

            this.AssociatedObject.ManipulationStarted -= AssociatedObject_ManipulationStarted;
            this.AssociatedObject.ManipulationDelta -= AssociatedObject_ManipulationDelta;
            this.AssociatedObject.ManipulationCompleted -= AssociatedObject_ManipulationCompleted;
        }

        private void AssociatedObject_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            SwipeGestureEventArgs args = new SwipeGestureEventArgs(ManipulationEvent.Started, e);

            base.InvokeActions(args);
        }

        private void AssociatedObject_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            SwipeGestureEventArgs args = new SwipeGestureEventArgs(ManipulationEvent.Delta, e);

            base.InvokeActions(args);
        }

        private void AssociatedObject_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            SwipeGestureEventArgs args = new SwipeGestureEventArgs(ManipulationEvent.Completed, e);

            base.InvokeActions(args);
        }
    }
}